import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { UserForm, Root, UserPageVO, UserQuery, UserPassForm, CodeForm, PostNewsPasswordForm } from './types';

// AxiosPromise使用 需要用 { data } 接收

/**
 * 登录成功后获取用户信息（昵称、头像、权限集合和角色集合）
 */
export function getUserInfo() {
  return request({
    url: '/api/sys/user/info',
    method: 'get'
  });
}

export function getUserDetail(data: any) {
  return request({
    url: '/api/plat/user/info',
    method: 'post',
    data
  });
}

/**
 * 获取C端用户分页列表
 *
 * @param queryParams
 */
export function getPlatUserList(
  data: any
) {
  return request({
    url: '/api/plat/user/list',
    method: 'post',
    data: data
  });
}

// 能力列表用户查询列表
export function getBcicapacityUserPage(data: any) {
  return request({
    url: '/api/plat/bcicapacity/bciCapacitypublisher',
    method: 'post',
    data
  });
}


/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function getUserPage(
  queryParams: UserQuery
) {
  return request({
    url: '/api/sys/user/list',
    method: 'post',
    data: queryParams
  });
}

/**
 * 获取用户表单详情
 *
 * @param userId
 */
export function getUserForm(userId: number): AxiosPromise<UserForm> {
  return request({
    url: '/api/v1/users/' + userId + '/form',
    method: 'get'
  });
}

/**
 * 添加用户
 *
 * @param data
 */
export function addUser(data: any) {
  return request({
    url: '/api/sys/user/add',
    method: 'post',
    data: data
  });
}

/**
 * 修改用户
 *
 * @param id
 * @param data
 */
export function updateUser(data: UserForm) {
  return request({
    url: '/api/sys/user/update',
    method: 'post',
    data: data
  });
}

/**
 * 修改用户状态
 *
 * @param id
 * @param status
 */
export function updateUserStatus(id: number, status: number) {
  return request({
    url: '/api/v1/users/' + id + '/status',
    method: 'patch',
    params: { status: status }
  });
}

/**
 * 修改用户密码
 *
 * @param data
 */
export function updateUserPassword(data: UserPassForm) {
  return request({
    url: '/api/sys/user/reSetPassword',
    method: 'post',
    data
  });
}

/**
 * 删除用户
 *
 * @param id
 */
export function deleteUsers(id: number) {
  return request({
    url: '/api/sys/user/delete',
    method: 'post',
    data: {
      id: id
    }
  });
}

/**
 * 批量删除用户
 *
 * @param ids
 */
export function deleteListUsers(ids: number[]) {
  return request({
    url: '/api/sys/user/batchdelete',
    method: 'post',
    data: {
      ids
    }
  });
}

/**
 * 下载用户导入模板
 *
 * @returns
 */
export function downloadTemplateApi() {
  return request({
    url: '/api/sys/excel/create',
    method: 'post'
  });
}

/**
 * 导出用户
 *
 * @param queryParams
 * @returns
 */
export function exportUser(queryParams: object) {
  return request({
    url: '/api/sys/excel/query',
    method: 'post',
    data: queryParams
  });
}

/**
 * 导入用户
 *
 * @param file
 */
export function importUser(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return request({
    url: '/api/sys/excel/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

/**
 * 发送手机验证码
 *
 * @param data
 * @returns
 */
export function postCode(data: CodeForm) {
  return request({
    url: '/api/password/reSetPassword/sms',
    method: 'post',
    data
  });
}

/**
 * 登录发送验证码
 */
export function loginCode(data: any) {
  return request({
    url: '/api/plat/user/loginByPhoneReturnCode',
    method: 'post',
    // headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
    data
  });
}

/**
 * 手机和验证码验证
 *
 * @param data
 * @returns
 */
export function postPhoneCode(data: CodeForm) {
  return request({
    url: '/api/password/reSetPassword/sms/verify',
    method: 'post',
    data
  });
}

/**
 * 重置密码
 *
 * @param data
 * @returns
 */
export function postNewsPassword(data: PostNewsPasswordForm) {
  return request({
    url: '/api/password/reSetPassword/sms/action',
    method: 'post',
    data
  });
}

